/**
 * 矩形类，参数可选，默认都为0
 * @author zhouzhanglin
 * @param x 默认为0
 * @param y 默认为0
 * @param width 默认为0
 * @param height 默认为0
 * @returns {Rectangle}
 */
Rectangle = function(x , y , width , height)
{
	this.x = x||0 ;
	this.y = y||0 ;
	this.width = width||0;
	this.height= height||0;
};
/**
 * 此矩形区域是否包含此点
 * @param x
 * @param y
 * @returns {Boolean}
 */
Rectangle.prototype.contains = function(x, y)
{
	if(x>= this.x && y>=this.y && x<= this.x+this.width && y<= this.y +this.height ){
		return true;
	}
	return false ;
};
/**
 * 此矩形区域是否包含此点
 * @param point
 * @returns {Boolean}
 */
Rectangle.prototype.containsPoint = function(point)
{
	return this.contains(point.x,point.y);
};
/**
 * 此矩形区域是否包含此区域
 * @param rect
 * @returns {Boolean}
 */
Rectangle.prototype.containsRect = function(rect)
{
	if(this.contains(rect.x,rect.y)) return true;
	if(this.contains(rect.x+rect.width,rect.y)) return true;
	if(this.contains(rect.x,rect.y+rect.height)) return true;
	if(this.contains(rect.x+rect.width,rect.y+rect.height)) return true;
	return false;
};
/**
 * 是否为空矩形，宽和高都为0
 * @returns {Boolean}
 */
Rectangle.prototype.isEmpty=function()
{
	return this.width==0 && this.height==0 ;
};
/**
 * 两个矩形的位置和大小 是否相同
 * @param toCompareRect
 * @returns {Boolean}
 */
Rectangle.prototype.equals = function(toCompareRect)
{
	return this.x==toCompareRect.x && this.y==toCompareRect.y && this.width==toCompareRect.width && this.height==toCompareRect.height;
};
/**
 * 两个矩形是否有相交
 * @param rect
 * @returns {Boolean}
 */
Rectangle.prototype.intersects = function(rect)
{
	return (this.x <= rect.x + rect.width && rect.x <= this.x + this.width &&
			this.y <= rect.y + rect.height && rect.y <= this.y + this.height);
};
/**
 * 两个矩形的相交区域
 * @param rect
 * @returns 没有相交的话，返回null
 */
Rectangle.prototype.intersection = function(rect)
{
	var x0 = Math.max(this.x, rect.x);
	var x1 = Math.min(this.x + this.width, rect.x + rect.width);

	if(x0 <= x1)
	{
		var y0 = Math.max(this.y, rect.y);
		var y1 = Math.min(this.y + this.height, rect.y + rect.height);

		if(y0 <= y1)
		{
			return new Rectangle(x0, y0, x1 - x0, y1 - y0);
		}
	}
	return null;
};
/**
 * 将两个矩形区域合并起来，组成一个大的矩形
 * @param rect
 */
Rectangle.prototype.mergeRect = function( rect)
{
	var minx = Math.min(this.x, rect.x);
	var miny = Math.min(this.y,rect.y);
	var maxx = Math.max(this.x+this.width, rect.x+rect.width);
	var maxy = Math.max(this.y+this.height, rect.x+rect.height);
	this.x = minx;
	this.y = miny ;
	this.width = maxx-minx;
	this.height = maxy-miny ;
};
/**
 * 复制
 * @returns {Rectangle}
 */
Rectangle.prototype.clone = function()
{
	return new Rectangle(this.x, this.y, this.width, this.height);	
};
/**
 * 输出信息
 */
Rectangle.prototype.toString = function()
{
	return "(x=" + this.x + ", y=" + this.y + ", width=" + this.width + ", height=" + this.height + ")";	
};
